package lotus.blog.lotusblog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lotus.blog.lotusblog.mapper.ArticleMapper;
import lotus.blog.lotusblog.mapper.UserMapper;
import lotus.blog.lotusblog.pojo.ArcticleVO;
import lotus.blog.lotusblog.pojo.Article;
import lotus.blog.lotusblog.pojo.User;
import lotus.blog.lotusblog.utils.JwtUtil;
import lotus.blog.lotusblog.utils.ResultTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Result;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    JwtUtil jwtUtil;
    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    UserMapper userMapper;
    @PostMapping("/add")
    public ResultTemplate add(@RequestBody Article article, HttpServletRequest request){
        try{
            int insert = articleMapper.insert(article);

        }catch(Exception e){
            e.printStackTrace();
            ResultTemplate.fail("提交错误");
        }

        return ResultTemplate.succ("提交成功",null);
    }
    @GetMapping("/getList")
    public ResultTemplate getList(@RequestParam("username")String username){
        List<Map<String, Object>> articleSampleList = articleMapper.getArticleSampleList(username);
        return ResultTemplate.succ("获取成功",articleSampleList);
    }
    @GetMapping("/get")
    public ResultTemplate get(@RequestParam("articleid") int articleid){
        Article article = articleMapper.selectById(articleid);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("article",article);
        User user = userMapper.selectById(article.getUserId());
        user.setPassword(null);
        map.put("user",user);
        return ResultTemplate.succ("获取成功",map);
    }
    @GetMapping("/delete")
    public ResultTemplate delete(@RequestParam("articleid") int articleid,HttpServletRequest request){
        User user = jwtUtil.getUserBytoken(request);
        Article article = articleMapper.selectById(articleid);
        if (user.getId()!=article.getUserId()){
            return ResultTemplate.authFail("非法信息");
        }
        try{
            int i = articleMapper.deleteById(article);
            if (i!=1){
                return ResultTemplate.fail("为查找到用户");
            }
        }catch (Exception e){
            e.printStackTrace();
            return  ResultTemplate.fail("删除失败");
        }
        return ResultTemplate.succ("删除成功",null);
    }
    @PostMapping("/update")
    public ResultTemplate update(@RequestBody Article article,HttpServletRequest request){
        User user = jwtUtil.getUserBytoken(request);
        if (user.getId()!=article.getUserId()){
            return ResultTemplate.authFail("非法信息");
        }
        try{
            articleMapper.updateById(article);
        }catch (Exception e){
            e.printStackTrace();
            return  ResultTemplate.fail("删除失败");
        }
        return ResultTemplate.succ("删除成功",null);
    }
}
